package com.jingdianjichi.project.domain.handler.subject;

import com.google.common.base.Preconditions;
import com.jingdianjichi.project.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.project.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.project.domain.bo.SubjectAnswerBo;
import com.jingdianjichi.project.domain.bo.SubjectInfoBo;
import com.jingdianjichi.project.domain.bo.SubjectOptionBo;
import com.jingdianjichi.project.domain.convert.RadioSubjectConverter;
import com.jingdianjichi.project.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.project.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * ClassName:RadioTypeHandler
 * Package:com.jingdianjichi.subject.domain.handler.subject
 * Description:
 *
 * @Author fst
 * @Create 2024/9/29 13:29
 * @Version 1.0
 */


/**
 * 单选题目
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler
{
    @Resource
    private SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo)
    {
        //单选题目的插入
        List<SubjectRadio> subjectRadioList=new LinkedList<>();

        Preconditions.checkNotNull(subjectRadioList,"题目列表不能为空");
      subjectInfoBo.getOptionList().forEach(option -> {
          SubjectRadio subjectRadio= RadioSubjectConverter.INSTANCE.convertBoTOEntity(option);
          subjectRadio.setSubjectId(subjectInfoBo.getId());
          subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
          subjectRadioList.add(subjectRadio);
        });

      subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBo query(int subjectId)
    {
        SubjectRadio subjectRadio=new SubjectRadio();
        subjectRadio.setSubjectId(Long.valueOf(subjectId));
        List<SubjectRadio> result = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBo> subjectAnswerBOList = RadioSubjectConverter.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBo subjectOptionBO = new SubjectOptionBo();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }

}
